/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _TANSU_HTTP_H_
#define _TANSU_HTTP_H_

#include "tansu_status.h"
#include "tansu_util.h"

#ifdef __cplusplus
extern "C" {
#endif

struct tansu_http_request_header {
    char *method;
    char *uri;
    char *version;
    struct tansu_keyvals headers;
};

struct tansu_http_response_header {
    char *version;
    int  status;
    char *readable;
    struct tansu_keyvals headers;
};

/**
 * Creates a request header instance.
 */
struct tansu_http_request_header *tansu_http_request_header_new();

/**
 * Creates a response header instance.
 */
struct tansu_http_response_header *tansu_http_response_header_new();

/**
 * Sets the request information to the specified instance.
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_MEMORY_ERROR
 */
tansu_status_t
tansu_http_request_header_set(struct tansu_http_request_header *req,
                              const char *method,
                              const char *uri,
                              const char *version);

/**
 * Sets the response information to the specified instance.
 * 
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_MEMORY_ERROR
 */
tansu_status_t
tansu_http_response_header_set(struct tansu_http_response_header *response,
                               const char *version,
                               int status,
                               const char *readable);

/**
 * Appends or updates the specified header.
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_MEMORY_ERROR
 */
tansu_status_t
tansu_http_header_add(struct tansu_keyvals *head,
                      const char *name,
                      const char *value);

/**
 * Removes the specified header.
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_NOT_FOUND
 */
tansu_status_t
tansu_http_header_remove(struct tansu_keyvals *head, const char *name);

const char *
tansu_http_header_get(const struct tansu_keyvals *head, const char *name);

void
tansu_http_header_clear(struct tansu_keyvals *head);

void
tansu_http_request_header_delete(struct tansu_http_request_header *req);

void
tansu_http_response_header_delete(struct tansu_http_response_header *response);

/**
 * @param[in] headers
 * @param[inout] text
 *
 * @retval 0 need more headers
 * @retval 1 all headers have been read
 */
int
tansu_http_parse_lines(struct tansu_keyvals *headers, char **text);

/**
 * @param[inout] text
 *
 * @return request header instance
 */
struct tansu_http_request_header *
tansu_http_parse_request_header(char **text);

/**
 * @param[inout] text
 *
 * @return response header instance
 */
struct tansu_http_response_header *
tansu_http_parse_response_header(char **text);

/**
 * Parses http request line.
 *
 * @param[inout] line request line to be parsed
 * @param[out] method
 * @param[out] uri
 * @param[out] version
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_INVALID_FORMAT
 */
tansu_status_t
tansu_http_parse_request_line(char *line,
                              char **method,
                              char **uri,
                              char **version);

/**
 * Parses http response line.
 *
 * @param[inout] line response line to be parsed
 * @param[out] version
 * @param[out] status
 * @param[out] readable
 *
 * @retval TANSU_SUCCESS
 * @retval TANSU_INVALID_ARGUMENT
 * @retval TANSU_INVALID_FORMAT
 */
tansu_status_t
tansu_http_parse_response_line(char *line,
                               char **version,
                               int *status,
                               char **readable);

tansu_status_t
tansu_http_parse_header_line(char *line, char **name, char **value);



#ifdef __cplusplus
}
#endif

#endif /* _TANSU_HTTP_H_ */

